﻿using Hotel.DAL;
using Hotel.Model.House;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hotel.BLL
{
    public class HouseBLL
    {
        HouseDAL dal;
        public HouseBLL(HouseDAL dal)
        {
            this.dal = dal;
        }

        /// <summary>
        /// 房型的增加
        /// </summary>
        /// <param name="info">房型的实体</param>
        /// <returns>影响的行数</returns>
        public int PostHouseTypeAdd(T_House_Type info)
        {
            try
            {
                int res = dal.SearchHouseTypeName(info.HY_Name);
                if(res > 0)
                {
                    //表示房型的名称重复
                    return -1;
                }
                else
                {
                    int result = dal.PostHouseTypeAdd(info);
                    if(result > 0)
                    {
                        //添加成功
                        return 1;
                    }
                    else
                    {
                        //表示添加失败
                        return -2;
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 获取所有的房型
        /// </summary>
        /// <param name="totalPage">总页数</param>
        /// <param name="totalCount">总条数</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>返回符合条件的数据</returns>
        public List<T_House_Type> GetHouseTypeList(out int totalPage, out int totalCount, int pageIndex = 1, int pageSize = 2)
        {
            try
            {
                //获取所有显示的数据
                List<T_House_Type> list = dal.GetHouseTypeList(out totalPage,out totalCount,pageIndex, pageSize);

                return list;

            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 根据id获取数据  实现数据的反填
        /// </summary>
        /// <param name="HtId">房型id</param>
        /// <returns></returns>
        public T_House_Type GetHouseTypeById(int HtId)
        {
            try
            {
                // T_House_Type  info = db.T_House_Type.Where(c=>c.HY_Id == HtId).FirstOrDefault();
                T_House_Type info = dal.GetHouseTypeById(HtId);
                return info;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 根据id进行修改某一条数据（对象）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int PutHouseTypeById(T_House_Type info)
        {
            try
            {
                int res = dal.PutHouseTypeById(info);   
                if(res > 0)
                {
                    //修改成功
                    return 1;
                }
                else
                {
                    //修改失败
                    return -2;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 根据id修改房型状态
        /// </summary>
        /// <param name="HTid"></param>
        /// <param name="HTstate"></param>
        /// <returns></returns>
        public int PutHouseTypeStateById(int HTid, int HTstate)
        {
            try
            {
                int res = dal.PutHouseTypeStateById(HTid, HTstate);
                if(res > 0)
                {
                    return 1;
                }
                else
                {
                    return -2;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 根据id进行删除一条记录
        /// </summary>
        /// <param name="HTid">房型id</param>
        /// <returns></returns>

        public int DeleteHouseTypeById(int HTid)
        {
            try
            {
               int res = dal.DeleteHouseTypeById(HTid);
                if(res > 0)
                {
                    return 1;
                }
                else
                {
                    return -2;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 实现批量删除
        /// </summary>
        /// <param name="idsArr"></param>
        /// <returns></returns>
        public int DeleteAllHouseTypeById(string idsArr)
        {
            try
            {
                int res = dal.DeleteAllHouseTypeById(idsArr);
                if(res > 0)
                {
                    return 1;
                }
                else
                {
                    return -2;  
                }

            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 获取房态
        /// </summary>
        /// <returns>返回符合条件的数据</returns>
        public List<T_House_State> GetHouseStateList()
        {
            try
            {
                List<T_House_State> list = dal.GetHouseStateList();
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 增加房间号
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int PostAddHouseNo(T_House_No info)
        {
            try
            {
                //1.先判断一下房间号重复不重复
                int res = dal.SeachHouseNo(info.HN_Name,info.StoreId);
                if(res > 0)
                {
                    return -1;
                }
                else
                {
                    int result = dal.PostAddHouseNo(info);
                    if(result > 0)
                    {
                        return 1;
                    }
                    else
                    {
                        return -2;
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 房间号分页
        /// </summary>
        /// <param name="totalPage">总页数</param>
        /// <param name="totalCount">总条数</param>
        /// <param name="houseNo">房间号</param>
        /// <param name="htId">房型</param>
        /// <param name="storeId">门店</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns></returns>
        public List<newHouseNo> GetHouseNoInfos(out int totalPage, out int totalCount, string? houseNo = "", int? htId = null, int? storeId = null, int pageIndex = 1, int pageSize = 10)
        {
            try
            {
                //1.查询所有的房间
                var list = dal.GetHouseNoInfos(out totalPage,out totalCount,houseNo,htId,storeId,pageIndex,pageSize);

                return list;

            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 获取所有的空闲房间
        /// </summary>
        /// <returns></returns>
        public IQueryable GetHouseState(int? htId = null, int? storeId = null)
        {
            try
            {
                var list = dal.GetHouseState(htId, storeId);

                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 找某个门店下的房间变成已入住就行
        /// </summary>
        /// <param name="hsId"></param>
        /// <param name="storeId"></param>
        /// <returns></returns>
        public int GetHouseNoStateStore(int hsId, int storeId)
        {
            try
            {
                var res = dal.GetHouseNoStateStore(hsId, storeId);
                if(res > 0)
                {
                    return 1;
                }
                else
                {
                    return -2;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 增加房价计划
        /// </summary>
        /// <param name="info">房价计划实体类</param>
        /// <returns>返回影响的行数</returns>
        public int PostAddHousePrice(T_House_Price info)
        {
            try
            {
                int res = dal.SearchHousePriceName(info.PH_Name, info.HY_Id);
                if(res > 0)
                {
                    //说明房价名称重复
                    return -1;
                }
                else
                {
                    int result = dal.PostAddHousePrice(info);
                    if(result > 0)
                    {
                        //增加成功
                        return 1;
                    }
                    else
                    {
                        //增加失败
                        return -2;
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 根据房型、房价计划名称  开始时间和结束时间  分页
        /// </summary>
        /// <returns></returns>
        public IQueryable GetHousePriceList()
        {
            try
            {
                var list = dal.GetHousePriceList();
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 房间删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteHouseNo(int id)
        {
            try
            {
               int res = dal.DeleteHouseNo(id);
                if(res > 0)
                {
                    return 1;
                }
                else
                {
                    return -2;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 修改房间的状态
        /// </summary>
        /// <param name="houseId"></param>
        /// <param name="houseStateId"></param>
        /// <returns></returns>
        public int PutHouseNoState(int houseId, int houseStateId)
        {
            try
            {
               int res = dal.PutHouseNoState(houseId, houseStateId);
                if (res > 0)
                {
                    return 1;
                }
                else
                {
                    return -2;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
